package com.amorcloud.pig.utils;

import android.text.TextUtils;
import android.util.Log;

import com.amorcloud.pig.MyApp;
import com.amorcloud.pig.bean.CheckBean;
import com.amorcloud.pig.bean.PigInfoBean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {

    public static final String BASE_DATA_INTERFACE= "/porksystem/api/miPigInfoManagerSaveDataToTB.jsp";
    public static final String GET_GROUP_NO_INTERFACE = "/porksystem/api/miPigInfoManagerGetGroupNo.jsp";
    public static final String GET_DEVICE_NO_INTERFACE = "/porksystem/api/miPigInfoManagerGetRFIDNumber.jsp";
    public static final String GET_SELL_NO_INTERFACE = "/porksystem/api/miPigInfoManagerGetSellNo.jsp";
    public static final String CHECK_INTERFACE = "/porksystem/api/miPigInfoManagerCheckData.jsp";
    public static final String CHECK_DATA_UPLOAD_INTERFACE = "/porksystem/api/miPigInfoManagerCheckUp.jsp";
    public static final String SELL_DATA_UPLOAD_INTERFACE = "/porksystem/api/miPigInfoManagerSellUp.jsp";
    public static final String DEVICE_STAUS_INTERFACE ="/porksystem/api/miPigInfoManagerJudgeCheckData.jsp";

    private static String sendPost(String url, String param) {
        Log.d("leihuiming", "url=" + url);
        Log.d("leihuiming", "param=" + param);

        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 设置连接超时时间
            conn.setConnectTimeout(5 * 1000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            if(!TextUtils.isEmpty(param)) {
                out.print(param);
            }
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static String uploadBasePigData(PigInfoBean pigInfoBean) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("ID=" + pigInfoBean.getId() + "&");
        stringBuffer.append("FarmID=" + pigInfoBean.getFarmID() + "&");
        stringBuffer.append("DateTime=" + pigInfoBean.getDateTime() + "&");
        stringBuffer.append("Sow=" + pigInfoBean.getSow() + "&");
        stringBuffer.append("UHFID=" + pigInfoBean.getUHFID() + "&");
        stringBuffer.append("HogID=" + pigInfoBean.getHogID() + "&");
        stringBuffer.append("Sex=" + pigInfoBean.getSex() + "&");
        stringBuffer.append("LTeat=" + pigInfoBean.getLTeat() + "&");
        stringBuffer.append("RTeat=" + pigInfoBean.getRTeat() + "&");
        stringBuffer.append("PrimaryW=" + pigInfoBean.getPrimaryW() + "&");
        stringBuffer.append("WeaningW=" + pigInfoBean.getWeaningW() + "&");
        stringBuffer.append("Lon=" + pigInfoBean.getLon() + "&");//经度
        stringBuffer.append("Lat=" + pigInfoBean.getLat()); //纬度
        return sendPost(MyApp.getUrl() + BASE_DATA_INTERFACE, stringBuffer.toString());
    }

    public static String uploadCheckData(String checkId, String comment) {
        String param = "ID=" + checkId + "&Comment=" + comment;
        return sendPost(MyApp.getUrl() + CHECK_DATA_UPLOAD_INTERFACE, param);
    }

    public static String uploadSellData(String checkId, String comment) {
        String param = "ID=" + checkId + "&Comment=" + comment;
        return sendPost(MyApp.getUrl() + SELL_DATA_UPLOAD_INTERFACE, param);
    }

    public static String getGroupNo(String farmName) {
        return sendPost(MyApp.getUrl() + GET_GROUP_NO_INTERFACE, "FarmName=" + farmName);
    }

    public static String getDeviceNo() {
        return sendPost(MyApp.getUrl() + GET_DEVICE_NO_INTERFACE, "");
    }

    public static String getSellNo(String groupNo) {
        return sendPost(MyApp.getUrl() + GET_SELL_NO_INTERFACE, "GroupNo=" + groupNo);
    }

    /**
     *
     * @param checkBean 盘点信息对象，获取盘点参数
     * @param startOrStop 1：开始盘点 0：停止盘点
     * @return
     */
    public static String pigCheck(CheckBean checkBean, int startOrStop) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("RfidNumber=" + checkBean.getRfidNumber() + "&"); //盘点设备号
        buffer.append("CheckType=" + checkBean.getCheckType() + "&");  //盘点类别
        buffer.append("Check=" + startOrStop + "&");      //开始停止
        if( checkBean.getCheckType() == 4) {
            buffer.append("Vecile=" + checkBean.getVecile() + "&");     //车牌号
        }
        buffer.append("OddNumber=" + checkBean.getOddNumber() );  //单号
        return sendPost(MyApp.getUrl() + CHECK_INTERFACE, buffer.toString());
    }

    /**
     * 查看设备状态
     * @param deviceNum
     * @return
     */
    public static String checkDeviceStatus(String deviceNum) {
        return sendPost(MyApp.getUrl() + DEVICE_STAUS_INTERFACE, "RfidNumber=" + deviceNum);
    }

}
